package com.gene.service.impl;

import com.gene.mapper.TbItemDescMapper;
import com.gene.mapper.TbItemMapper;
import com.gene.pojo.TbItem;
import com.gene.pojo.TbItemDesc;
import com.gene.pojo.TbItemExample;
import com.gene.pojo.entity.Product;
import com.gene.service.ITbItemService;
import com.gene.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2018/1/15.
 */
@Service
public class TbItemServiceImpl implements ITbItemService {
    @Autowired
    private TbItemMapper mapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;

    /**
     *  商品的查询，带多条件查询
     *  参数为tbItem
     *  1: 参数  id（商品id） 查询单个数据
     *  2：参数  hot=1   查询热门的数据  有排序
     *  3：参数  cid   按照cid（类型）来查询
     *
     *  注意 所有数据 status（商品状态）=1（正常）
     */
    @Override
    public List<TbItem> tbItemLsit(TbItem tbItem) {
        List<TbItem> list = mapper.tbItemLsit(tbItem);
        return list;
    }

    /**
     * 根据商品id联合查询   返回结果为Product类 = TbItem  +TbItemDesc
     * @param tbItem
     * @return
     */
    @Override
    public Product selectId(TbItem tbItem) {
        Product product = mapper.selectId(tbItem);
        return product;
    }

    @Override
    public TbItem add(TbItem item,String desc) {
        Long randomCode = (long) ((Math.random() * 9 + 1) * 1000);
        Date date = new Date();
        item.setCreated(date);
        item.setUpdated(date);
        item.setStatus((byte) 1);
        String s = new Date().getTime() + "";
        String ss = randomCode+ "";
        String s1 = s + ss;
        item.setId(Long.valueOf(s1));
        mapper.insert(item);
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(Long.valueOf(s1));
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        itemDescMapper.insert(itemDesc);
        return item;
    }

    @Override
    public TbItem queryid(Long id) {
        TbItemExample example=new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo((byte) 1);
        criteria.andIdEqualTo(id);
        List<TbItem> list = mapper.selectByExample(example);
        if(list==null||list.size()==0){
            return  null;
        }
        return list.get(0);
    }

    @Override
    public PageResult<TbItem> queryCidList(Integer pageNum, Integer pageSize, Long cid) {
        PageHelper.startPage(pageNum,pageSize);//构造分页对象给参数，当前页跟每页显示数
        TbItemExample example=new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andCidEqualTo(cid);
        criteria.andStatusEqualTo((byte) 1);
        List<TbItem> list = mapper.selectByExample(example);
        PageInfo<TbItem>lists=new PageInfo<TbItem>(list);
        System.out.println(lists);
        PageResult<TbItem>result=new PageResult<>();
        result.setList(list);
        //result.setTotalPage(lists.getPages());
        return result;
    }

    @Override
    public void edit(TbItem item,String desc) {
        item.setUpdated(new Date());
        mapper.updateByPrimaryKeySelective(item);
        if(null!=desc){
            TbItemDesc tbItemDesc=new TbItemDesc();
            tbItemDesc.setItemId(item.getId());
            tbItemDesc.setItemDesc(desc);
            tbItemDesc.setUpdated(new Date());
            itemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
        }
    }

    @Override
    public void delete(Long id) {
        TbItem tbItem=new TbItem();
        tbItem.setStatus((byte) 3);
        mapper.updateByPrimaryKeySelective(tbItem);
    }


    /**
     *  商品的查询，按照title(产品名称)模糊搜索
     *
     *  注意 所有数据 status（商品状态）=1（正常）
     */
    @Override
    public List<TbItem> title(TbItem item) {
        TbItemExample example=new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo((byte) 1);
        criteria.andTitleLike("%"+item.getTitle()+"%");
        List<TbItem> list = mapper.selectByExample(example);
        return list;
    }
}
